using System.Linq;
using Volo.Abp.Localization;
using Volo.Abp.Authorization.Permissions;
using Joy.Erp.BasicArchives.Localization;

namespace Joy.Erp.BasicArchives.Permissions
{
    public class IntentOrderTypePermissionDefinitionPovider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(BasicArchivesPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(BasicArchivesPermissions.Menus, L("Permission:Menus"));
            }

            var salesManagementMenu = menus.GetPermissionOrNull(BasicArchivesPermissions.BasicArchivesMenu.SalesManagement);
            if (salesManagementMenu == null)
            {
                salesManagementMenu = menus.AddPermission(BasicArchivesPermissions.BasicArchivesMenu.SalesManagement, L("Permission:Menus.BasicArchives.SalesManagement"));
            }

            var parentMenu = salesManagementMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.IntentOrderType.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = salesManagementMenu.AddChild(BasicArchivesPermissions.IntentOrderType.ParentMenu, L("Permission:Menus.BasicArchives.SalesManagement.Sales"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.IntentOrderType.Menu);
            if (menu == null)
            {
                menu = parentMenu.AddChild(BasicArchivesPermissions.IntentOrderType.Menu, L("Permission:Menus.BasicArchives.SalesManagement.Sales.IntentOrderType"));
            }

            var group = context.GetGroupOrNull(BasicArchivesPermissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup(BasicArchivesPermissions.GroupName, L("Permission:BasicArchives"));
            }

            var pIntentOrderTypeDefault = group.AddPermission(BasicArchivesPermissions.IntentOrderType.Default, L("Permission:BasicArchives.Sales.IntentOrderType"));
            var pIntentOrderTypeQuery = pIntentOrderTypeDefault.AddChild(BasicArchivesPermissions.IntentOrderType.Query, L("Permission:Query"));
            var pIntentOrderTypeEdit = pIntentOrderTypeDefault.AddChild(BasicArchivesPermissions.IntentOrderType.Edit, L("Permission:Edit"));
            var pCreate = pIntentOrderTypeEdit.AddChild(BasicArchivesPermissions.IntentOrderType.Create, L("Permission:Create"));
            var pUpdate = pIntentOrderTypeEdit.AddChild(BasicArchivesPermissions.IntentOrderType.Update, L("Permission:Update"));
            var pDelete = pIntentOrderTypeEdit.AddChild(BasicArchivesPermissions.IntentOrderType.Delete, L("Permission:Delete"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
}